//
//  FQDNavigationView.m
//  FakeQDaily
//
//  Created by mewxiv on 9/8/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDNavigationView.h"
#import "FQDBlurView.h"

#import "Masonry.h"

@interface FQDNavigationView ()

@property (nonatomic, strong) FQDBlurView *blurView;

@end

@implementation FQDNavigationView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _showBlurBg = NO;
        [self prepareUI];
        [self prepareConstraints];
    }
    return self;
}

- (void)prepareUI
{
    UIImage *backImage = [UIImage imageNamed:@"back"];
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setImage:backImage forState:UIControlStateNormal];
    [_backButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 20.0f)];
    [self addSubview:_backButton];
    
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = FQDRegularFontWithSize(17.0f);
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.textColor = [UIColor whiteColor];
    [self addSubview:_titleLabel];
}

- (void)prepareConstraints
{
    WeakSelf;
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(25);
        make.left.equalTo(weakSelf).offset(15);
        make.size.mas_equalTo(CGSizeMake(50, 32));
    }];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf).offset(20);
        make.centerX.equalTo(weakSelf);
        make.size.mas_equalTo(CGSizeMake(280, 44));
    }];
}

- (UIButton *)rightButton
{
    if (!_rightButton) {
        _rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightButton setTitleColor:FQDHighlightColor forState:UIControlStateNormal];
        [self addSubview:_rightButton];
        
        WeakSelf;
        [_rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf).offset(-15);
            make.centerY.equalTo(weakSelf.backButton);
        }];
    }
    return _rightButton;
}

- (UIView *)blurView
{
    if (!_blurView) {
        _blurView = [[FQDBlurView alloc] init];
        _blurView.hidden = YES;
        [self addSubview:_blurView];
        [self sendSubviewToBack:_blurView];
        
        WeakSelf;
        [_blurView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(weakSelf);
        }];
    }
    return _blurView;
}

- (void)setShowBlurBg:(BOOL)showBlurBg
{
    _showBlurBg = showBlurBg;
    
    self.blurView.hidden = !showBlurBg;
}

@end
